package com.yoki.cloud.common.protocol.portal.v1.sender;

import com.yoki.cloud.common.protocol.portal.exception.PortalException;
import com.yoki.cloud.common.protocol.portal.msg.PortalV1Msg;
import com.yoki.cloud.common.protocol.portal.v1.builder.PortalV1PapMsgBuilder;
import com.yoki.cloud.common.protocol.portal.v1.parser.PortalV1MsgParser;
import lombok.Getter;
import lombok.Setter;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

/**
 * @author Sky$
 * @Description: TODO
 * @date 2017/10/31$ 17:57$
 */
public class PortalV1PapMsgSender {

    @Setter
    @Getter
    private int timeOut = 5000;

    @Setter
    @Getter
    private Integer receiverPort = null;

    /**
     * REQ_AUTH	0x03	Client----->Server	AbstractPortalMsg Server向AC设备发送的请求认证报文
     *
     * @param serialNo
     * @param loginIp
     * @param loginName
     * @param password
     * @param acIp
     * @param acPort
     * @return
     * @throws Exception
     */
    public byte[] sendReqAuth(int serialNo, String loginIp, String loginName, String password, String acIp, int acPort) throws IOException {
        DatagramSocket dataSocket = null;
        byte[] ackData = new byte[0];
        dataSocket = null;
        DatagramPacket requestPacket = null;
        PortalV1PapMsgBuilder builder = new PortalV1PapMsgBuilder();
        PortalV1Msg msg = builder.buildReqAuthMsg(serialNo, loginIp, loginName, password);
        byte[] msgBytes = msg.toByteArray();
        // 创建连接
        if (null != receiverPort) {
            dataSocket = new DatagramSocket(receiverPort);
        } else {
            dataSocket = new DatagramSocket();
        }
        // 创建发送数据包并发送给服务器
        requestPacket = new DatagramPacket(msgBytes, msgBytes.length, InetAddress.getByName(acIp), acPort);
        ackData = new byte[1024];
        // 接收服务器的数据包
        DatagramPacket receivePacket = new DatagramPacket(ackData, 32);
        // 设置请求超时3秒
        dataSocket.setSoTimeout(timeOut);
        dataSocket.send(requestPacket);
        dataSocket.receive(receivePacket);

        if (null != dataSocket) {
            dataSocket.disconnect();
            dataSocket.close();
        }
        return ackData;

    }

    /**
     * REQ_AUTH	0x03	Client----->Server	AbstractPortalMsg Server向AC设备发送的请求认证报文
     *
     * @param serialNo
     * @param loginIp
     * @param loginName
     * @param password
     * @param acIp
     * @param acPort
     * @return
     * @throws Exception
     */
    public PortalV1Msg sendAndParseReqAuth(int serialNo, String loginIp, String loginName, String password, String acIp, int acPort) throws PortalException, IOException {
        byte[] ackData = sendReqAuth(serialNo, loginIp, loginName, password, acIp, acPort);
        PortalV1MsgParser msgParser = new PortalV1MsgParser();
        PortalV1Msg reciveMsg = msgParser.parse(ackData);
        reciveMsg.parse(ackData);
        return reciveMsg;

    }

    /**
     * AbstractPortalMsg Server对收到的认证成功响应报文的确认报文；
     *
     * @param serialNo
     * @param loginIp
     * @param acIp
     * @param acPort
     * @throws Exception
     */
    public void sendAffAckAuth(int serialNo, String loginIp, String acIp, int acPort) throws IOException {
        DatagramSocket dataSocket = null;
        PortalV1PapMsgBuilder builder = new PortalV1PapMsgBuilder();
        PortalV1Msg msg = builder.buildAffAckAuthMsg(serialNo, loginIp);
        byte[] msgBytes = msg.toByteArray();
        // 创建连接
        if (null != receiverPort) {
            dataSocket = new DatagramSocket(receiverPort);
        } else {
            dataSocket = new DatagramSocket();
        }
        // 创建发送数据包并发送给服务器
        DatagramPacket requestPacket = new DatagramPacket(msgBytes, msgBytes.length, InetAddress.getByName(acIp), acPort);
        // 接收服务器的数据包
        dataSocket.send(requestPacket);
        if (null != dataSocket) {
            dataSocket.disconnect();
            dataSocket.close();
        }
    }


    /**
     * AbstractPortalMsg Server向AC设备发送的请求用户下线报文
     *
     * @param serialNo
     * @param loginIp
     * @param acIp
     * @param acPort
     * @return
     * @throws Exception
     */
    public byte[] sendReqLogout(int serialNo, String loginIp, String acIp, int acPort) throws IOException {
        DatagramSocket dataSocket = null;
        byte[] ackData = new byte[0];
        PortalV1PapMsgBuilder builder = new PortalV1PapMsgBuilder();
        PortalV1Msg msg = builder.buildReqLogoutMsg(serialNo, loginIp);
        byte[] msgBytes = msg.toByteArray();
        // 创建连接
        if (null != receiverPort) {
            dataSocket = new DatagramSocket(receiverPort);
        } else {
            dataSocket = new DatagramSocket();
        }
        // 创建发送数据包并发送给服务器
        DatagramPacket requestPacket = new DatagramPacket(msgBytes, msgBytes.length, InetAddress.getByName(acIp), acPort);
        ackData = new byte[1024];
        // 接收服务器的数据包
        DatagramPacket receivePacket = new DatagramPacket(ackData, 32);
        // 设置请求超时3秒
        dataSocket.setSoTimeout(timeOut);
        dataSocket.send(requestPacket);
        dataSocket.receive(receivePacket);
        if (null != dataSocket) {
            dataSocket.disconnect();
            dataSocket.close();
        }
        return ackData;


    }

    /**
     * AbstractPortalMsg Server向AC设备发送的请求用户下线报文
     *
     * @param serialNo
     * @param loginIp
     * @param acIp
     * @param acPort
     * @return
     * @throws Exception
     */
    public PortalV1Msg sendAndParseReqLogout(int serialNo, String loginIp, String acIp, int acPort) throws IOException, PortalException {
        byte[] ackData = sendReqLogout(serialNo, loginIp, acIp, acPort);
        PortalV1MsgParser msgParser = new PortalV1MsgParser();
        PortalV1Msg reciveMsg = msgParser.parse(ackData);
        reciveMsg.parse(ackData);
        return reciveMsg;

    }

}